home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AmigActive 10
/
AACD 10.iso
/
AACD
/
Magazine
/
Online
/
httpproxy
/
src
/
logging.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-08-20
|
7KB
|
316 lines
/*(( "Header" */
/*
* $Id: logging.c,v 1.6 1996/08/12 03:33:36 mshopf Exp mshopf $
*
* (c) 1995-96 Matthias Hopf
*
* Log file functions for httpproxy.
*
*/
/*
* $Log: logging.c,v $
* Revision 1.6 1996/08/12 03:33:36 mshopf
* logging Url from extra field of request_t now.
*
* Revision 1.5 1996/08/11 22:25:15 mshopf
* reworked debug messages.
*
* Revision 1.4 1996/07/30 13:57:03 mshopf
* output alignment fix.
*
* Revision 1.3 1996/07/17 16:42:42 mshopf
* added IoErr() error messages.
*
* Revision 1.2 1996/04/26 05:14:03 mshopf
* V0.13 alpha 5 fix.
*
* Revision 1.1 1996/04/24 03:20:13 mshopf
* Initial revision
*
*/
/*)) */
/*(( "Logfile format" */
/* Format of the logfile in EBNF:
*
* Logfile ::= { LogEntry }*
* LogEntry ::= { Special | Standard | Message | Error | WarnInfo } '\n'
*
* Special ::= '*' AnyText
* Standard ::= ' ' Connection Sp StdConnect
* Message ::= '.' Connection Sp MsgConnect
* Error ::= '-' { Connection Sp ErrConnect | ErrNoConnect }
* WarnInfo ::= '+' { Connection Sp WnIConnect | WnINoConnect }
*
* Connection ::= '#' Slot Sp HostAddr
* StdConnect ::= Status Sp [ Url ]
* MsgConnect ::= 'message' Sp [ Url ] Sp ' : ' InfoText
* ErrConnect ::= { 'error' | 'errmsg' } Sp [ Url ] Sp ' : ' InfoText
* WnIConnect ::= { 'warning' | 'info' } Sp [ Url ] Sp ' : ' InfoText
* ErrNoConnect ::= 'error : ' InfoText
* WnINoConnect ::= { 'warning' | 'info' } Sp ' : ' InfoText
*
* Slot ::= Number
* HostAddr ::= { INetAddr ':' Port } | ':proxy:'
* Status ::= 'cached' | 'new' | 'proxied' | 'queued'
* Url ::= 'URL=' AnyNoSpText
* InfoText ::= Reason [':' Errno]
*
* Port ::= Number
* Reason ::= AnyNoCoText
* Errno ::= AnyText
*
* Partial semi-formal description of all low-level symbols:
*
* Number ::= { nummeric }+
* INetAddr ::= { alphanummeric | '.' }+
* AnyNoSpText ::= { printable }*
* AnyNoCoText ::= { printable w/o ':' | ' ' }*
* AnyText ::= { printable | ' ' }*
* Sp ::= { ' ' }*
*/
/*)) */
/*(( "Includes/Constants" */
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "httpproxy.h"
#include "logging.h"
/*)) */
/*(( "Global Variables" */
static FILE *LogStream = NULL;
/*)) */
/*(( "assert()" */
#ifndef NDEBUG
void ASSERT (int x, const char *text, const char *file, const char *func, int line)
{
extern FILE *LogStream;
if (x)
return;
if (! text)
text = "unknown";
fprintf (stderr, "assertion (%s) failed in '%s' of '%s' on line %d\n", text, func, file, line);
debugraw (D_ALWAYS, ("assertion (%s) failed in '%s' of '%s' on line %d\n", text, func, file, line));
if (LogStream)
{
fprintf (LogStream, "*assertion (%s) failed in '%s' of '%s' on line %d\n", text, func, file, line);
fclose (LogStream);
LogStream = NULL;
}
ExitAll (20);
/*NOTREACHED*/
}
#endif /* NDEBUG */
/*)) */
/*(( "LogOpenFile()" */
void LogOpenFile (const char *Name)
{
if (LogStream)
LogCloseFile ();
if (! (LogStream = fopen (Name, "a+")))
{
fprintf (stderr, "cannot open logfile '%s': %s\n", Name, strerror (errno));
ExitAll (20);
}
}
/*)) */
/*(( "LogCloseFile()" */
void LogCloseFile (void)
{
if (LogStream)
fclose (LogStream);
LogStream = NULL;
}
/*)) */
/*(( "LogSpecial()" */
void LogSpecial (const char *Template, ...)
{
va_list Args;
assert (LogStream != NULL);
fputs ("*", LogStream);
debug (D_MSG, ("*"));
va_start (Args, Template);
vfprintf (LogStream, Template, Args);
vdebugraw (D_MSG, Template, Args);
va_end (Args);
fflush (LogStream);
}
/*)) */
/*(( "LogStd()" */
void LogStd (request_t *Req, const char *Status)
{
assert (Req != NULL);
if (Req->Flags & REQ_LOGGED)
return;
if (! Status)
{
LogErr (Req, L_ERROR, NULL, 0, "*** unknown error ***");
return;
}
if (Req->Flags)
Req->Flags |= REQ_LOGGED;
if (Req->Address[0])
{
fprintf (LogStream, " #%02d %-16s %-10s", Req-Requests, Req->Address, Status);
debug (D_MSG, ( " #%02d %-16s %-10s", Req-Requests, Req->Address, Status));
}
else
{
fprintf (LogStream, " #%02d :proxy: %-10s", Req-Requests, Status);
debug (D_MSG, ( " #%02d :proxy: %-10s", Req-Requests, Status));
}
if (Req->Url [0])
{
fprintf (LogStream, " URL=%s\n", Req->Url);
debugraw (D_MSG, ( " URL=%s\n", Req->Url));
}
else
{
fputs ("\n", LogStream);
debugraw (D_MSG, ("\n"));
}
fflush (LogStream);
}
/*)) */
/*(( "LogErr()" */
void LogErr (request_t *Req, const char *Status, const char *Url, int ErrNo, const char *Reason, ...)
{
va_list Args;
char IoErrTxt [128];
char Type;
assert (Reason != NULL);
va_start (Args, Reason);
Type = '+';
if (Status == L_ERROR)
{
Status = "error";
Type = '-';
}
if (Status == L_MSG)
{
Status = "message";
Type = '.';
}
if (Status == L_ERRMSG)
{
Status = "errmsg";
Type = '-';
}
if (! Req)
{
fprintf (LogStream, "%c%-10s : ", Type, Status);
debug (D_MSG, ( "%c%-10s : ", Type, Status));
vfprintf (LogStream, Reason, Args);
vdebugraw (D_MSG, Reason, Args);
if (ErrNo > 0)
{
fprintf (LogStream, ": %s\n", strerror (ErrNo));
debugraw (D_MSG, ( ": %s\n", strerror (ErrNo)));
errno = 0;
}
else if (ErrNo < 0)
{
Fault (IoErr (), "", IoErrTxt, 128);
fprintf (LogStream, "%s\n", IoErrTxt);
debugraw (D_MSG, ( "%s\n", IoErrTxt));
}
else
{
fputs ("\n", LogStream);
debugraw (D_MSG, ("\n"));
}
}
else
{
if (Req->Flags)
Req->Flags |= REQ_LOGGED;
if (Req->Address[0])
{
fprintf (LogStream, "%c#%02d %-16s %-10s", Type, Req-Requests, Req->Address, Status);
debug (D_MSG, ( "%c#%02d %-16s %-10s", Type, Req-Requests, Req->Address, Status));
}
else
{
fprintf (LogStream, "%c#%02d :proxy: %-10s", Type, Req-Requests, Status);
debug (D_MSG, ( "%c#%02d :proxy: %-10s", Type, Req-Requests, Status));
}
if (Url)
{
fprintf (LogStream, " URL=%s : ", Url);
debugraw (D_MSG, ( " URL=%s : ", Url));
}
else if (Req->Url [0])
{
fprintf (LogStream, " URL=%s : ", Req->Url);
debugraw (D_MSG, ( " URL=%s : ", Req->Url));
}
else
{
fputs (" : ", LogStream);
debugraw (D_MSG, (" : "));
}
vfprintf (LogStream, Reason, Args);
vdebugraw (D_MSG, Reason, Args);
if (ErrNo > 0)
{
fprintf (LogStream, ": %s\n", strerror (ErrNo));
debugraw (D_MSG, ( ": %s\n", strerror (ErrNo)));
errno = 0;
}
else if (ErrNo < 0)
{
Fault (IoErr (), "", IoErrTxt, 128);
fprintf (LogStream, "%s\n", IoErrTxt);
debugraw (D_MSG, ( "%s\n", IoErrTxt));
}
else
{
fputs ("\n", LogStream);
debugraw (D_MSG, ("\n"));
}
}
va_end (Args);
fflush (LogStream);
}
/*)) */